/*
 * DeleteServlet.java
 *
 * 6�~10��19�� �P�e|, �U��11:54
 */

package servlet;

import java.io.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.*;

import javax.servlet.*;
import javax.servlet.http.*;
import util.ConnectionManager;

/**
 *
 * @author HOME
 * @version
 */
public class DeleteServlet extends HttpServlet {
    
    /** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        String id = request.getParameter("itemID");
        String name = request.getParameter("name");
        Connection con = null;
        PreparedStatement update = null;
        try {
            con = ConnectionManager.connect();            
            update = con.prepareStatement("DELETE FROM model WHERE itemID=?;");            
            update.setString(1, id);
            update.executeUpdate();
            con.commit();
            request.getSession().setAttribute("complete_msg", "Item "+name+" has been deleted.");
            response.sendRedirect("completePage.jsp");
        } catch (Exception e) {
            
        } finally {
            util.Utilities.close(null, update, con);
        }
    }
    
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }
    
    /** Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }
    
    /** Returns a short description of the servlet.
     */
    public String getServletInfo() {
        return "Short description";
    }
    // </editor-fold>
}
